package com.bw.wxy.mvp.presenter;

import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.bw.wxy.common.App;
import com.bw.wxy.db.UserManager;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.ResponseUserEntity;
import com.bw.wxy.mvp.base.prsenter.BasePresenter;
import com.bw.wxy.mvp.contract.LoginContract;
import com.bw.wxy.mvp.model.entity.UserEntity;
import com.umeng.analytics.pro.r;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<LoginContract.ILoginModel, LoginContract.ILoginView> {
    @Inject
    public LoginPresenter(LoginContract.ILoginModel mModel, LoginContract.ILoginView mView) {
        super(mModel, mView);
    }

    public void getLogin(String phone, String pwd, int code) {
//        if (pwd.isEmpty()) {
//            Toast.makeText(App.getInstance().getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
//            return;
//        }
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("pwd", pwd);
        map.put("code", code);
//        map.put("sign","ae4e3566f409ba2878479f696e6ee19a");
        mModel.request(map,"").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }



    public void uploadhead(String file,int code){
//        List<UserEntity> userEntities = UserManager.getInstance().getDaoSession().loadAll(UserEntity.class);
        Map<String, Object> map = new HashMap<>();
        map.put("code",code);
        mModel.request(map,file)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseEntity baseEntity) {
                        mView.showHeadImg(baseEntity);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
//                        Log.e("asd",""+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateUser(String user_name,int user_sex,String img,int code){
        List<UserEntity> userEntities = UserManager.getInstance().getDaoSession().loadAll(UserEntity.class);
        Map<String,Object>map = new HashMap<>();
        map.put("user_id",userEntities.get(0).getUser_id());
        map.put("user_img",img);
        map.put("user_integral",1);
        map.put("user_name",user_name);
        map.put("user_phone",userEntities.get(0).getUser_phone());
        map.put("user_pwd",userEntities.get(0).getUser_pwd());
        map.put("user_sex",user_sex);
        map.put("code",code);
        mModel.request(map,"").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseEntity baseEntity) {
//                        LogUtils.json(UserManager.getInstance().getDaoSession().loadAll(UserEntity.class));
                        mView.showView(baseEntity);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
//        Log.i("wxy", "onNext: "+1);
        if (baseEntity instanceof ResponseUserEntity) {
//            Log.i("wxy", "onNext2: "+1);
//            UserManager.getInstance().getDaoSession().update(new UserEntity());
            ResponseUserEntity responseUserEntity = (ResponseUserEntity) baseEntity;
            UserManager.getInstance().getDaoSession().insert(responseUserEntity.getValues());
        }
        mView.showView(baseEntity);
    }
}

